import sys
import os
from fastapi import APIRouter, HTTPException, Form

# 添加项目根目录到Python路径
backend_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, backend_path)

# 修复导入路径
from api import system_log_api as email_log_api

router = APIRouter()

@router.get("/logs")
def get_email_logs():
    """获取邮件日志（无分页）"""
    try:
        return email_log_api.get_email_logs()
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@router.post("/logs")
def add_email_log(message: str = Form(...), level: str = Form("INFO")):
    """添加邮件日志"""
    try:
        # 如果level为None，则使用默认值"INFO"
        level = level or "INFO"
        return email_log_api.add_email_log(message, level)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@router.delete("/logs")
def clear_email_logs():
    """清空邮件日志"""
    try:
        return email_log_api.clear_email_logs()
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))